package com.cjack.frenchexcel.advise;

import com.cjack.frenchexcel.config.AjaxResult;
import com.cjack.frenchexcel.config.ErrorCodeDefine;
import com.cjack.frenchexcel.exception.CommonException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 统一异常处理
 */
@Slf4j
@RestControllerAdvice(annotations = RestController.class)
public class ControllerAdvice {

    @ExceptionHandler({CommonException.class})
    public AjaxResult handleCommonException(CommonException ex) {
        log.error("Got CommonException: [{}]", ex.getMessage(), ex);
        return AjaxResult.ERROR(ex.getCode(), ex.getMessage());
    }

    @ExceptionHandler({Exception.class})
    public AjaxResult handleException(Exception ex) {
        log.error("Got Exception [{}]", ex.getMessage(), ex);
        return AjaxResult.ERROR(ErrorCodeDefine.INTERFACE_COMMON_ERROR, ex.getMessage());
    }
}
